package com.EAIPClub.LiveStreamingManager.service.impl;

import com.EAIPClub.LiveStreamingManager.mapper.LiveStreamingManagerMapper;
import com.EAIPClub.LiveStreamingManager.service.LiveStreamingManagerWebRtcService;
import com.api.API.UserAuthDataManager.UserAuthDataManagerAPI;
import com.api.API.UserDataManager.UserDataManagerAPI;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerInfo;
import com.api.POJO.LiveStreamingManager.LiveStreamingManagerReturnMsg;
import com.api.POJO.UserAuthDataManager.UserAuthDataManagerReturnMsg;
import com.api.POJO.UserDataManager.Result.UserDataReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * @Author :
 * @Date : 2021/12/9
 */
@Service
@Slf4j
public class LiveStreamingManagerManagerWebRtcServiceImpl implements LiveStreamingManagerWebRtcService {
    private UserAuthDataManagerAPI authDataManagerAPI;

    @Autowired
    public void setAuthDataManagerAPI(UserAuthDataManagerAPI authDataManagerAPI) {
        this.authDataManagerAPI = authDataManagerAPI;
    }

    private UserDataManagerAPI dataManagerAPI;

    @Autowired
    public void setDataManagerAPI(UserDataManagerAPI dataManagerAPI) {
        this.dataManagerAPI = dataManagerAPI;
    }

    private LiveStreamingManagerMapper mapper;

    @Autowired
    public void setMapper(LiveStreamingManagerMapper mapper) {
        this.mapper = mapper;
    }

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public LiveStreamingManagerReturnMsg addNewLiveStreaming(String token) {
        LiveStreamingManagerReturnMsg returnMsg = new LiveStreamingManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("用户token:{},申请WebRtc直播.",token);
        log.info("即将通过token查询用户登录信息.token:{}",token);
        UserAuthDataManagerReturnMsg userAuthDataByToken = authDataManagerAPI.getUserAuthDataByToken(token);
        if (userAuthDataByToken==null){
            log.warn("通过token查询用户用户登录信息失败.返回为空.");
            Msg.add("查询用户用户登录信息失败!");
            returnMsg.setStatus(500);
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if (userAuthDataByToken.getStatus()!=200) {
            log.warn("通过token查询用户用户登录信息.返回信息:{}", userAuthDataByToken.getMsg());
            returnMsg.setStatus(500);
            Msg.add("查询用户登录信息失败!");
            return returnMsg;
        }
        log.info("通过token查询用户登录信息成功.用户用户登录信息:{}",userAuthDataByToken);
        String uid = userAuthDataByToken.getAuthData().getUid();
        log.info("即将通过uid查询用户基本信息.uid:{}",uid);
        UserDataReturnMsg userBaseInfoByUid = dataManagerAPI.findUserBaseInfoByUid(uid);
        if (userBaseInfoByUid == null){
            log.warn("通过用户uid查询用户基本信息失败!返回为空!");
            returnMsg.setStatus(500);
            Msg.add("查询用户基本信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        if(userBaseInfoByUid.getStatus()!=200){
            log.warn("通过用户uid查询用户基本信息失败!返回信息:{}",userBaseInfoByUid.getMsg());
            returnMsg.setStatus(500);
            Msg.add("查询用户基本信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("查询用户基本信息成功.用户基本信息:{}",userBaseInfoByUid);
        LiveStreamingManagerInfo webRtcLiveStreamingManagerInfoByToken = mapper.getWebRtcLiveStreamingInfoByToken(token);
        if(webRtcLiveStreamingManagerInfoByToken !=null){
            log.warn("检测到已有直播信息!直播信息:{}", webRtcLiveStreamingManagerInfoByToken);
            returnMsg.setStatus(200);
            returnMsg.setAddress(webRtcLiveStreamingManagerInfoByToken.getAddress());
            return returnMsg;
        }
        log.info("即将开始分配直播地址.");
        String webRtcAddress = createWebRtcAddress(token);
        if (webRtcAddress == null){
            log.warn("检测到分配WebRtc直播地址为空!");
            returnMsg.setStatus(500);
            Msg.add("分配WebRtc直播地址错误!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("本次分配rtmp地址为:"+webRtcAddress+".即将记录于数据库.");
        Date start = new Date();
        Boolean aBoolean = mapper.addNewWebRtcInfo(new LiveStreamingManagerInfo(null, userBaseInfoByUid.getUser().getEmail(), token, simpleDateFormat.format(start), webRtcAddress));
        if (!aBoolean){
            log.warn("记录数据库失败!");
            returnMsg.setStatus(500);
            Msg.add("分配WebRtc地址失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("记录数据库成功.本次业务结束.");
        returnMsg.setStatus(200);
        returnMsg.setAddress(webRtcAddress);
        return returnMsg;
    }

    @Override
    public LiveStreamingManagerReturnMsg deleteNewLiveStreaming(String token) {
        LiveStreamingManagerReturnMsg returnMsg = new LiveStreamingManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token删除用户WebRtc直播信息.");
        if (token == null || token.equals("")){
            log.warn("检测到token为空!");
            returnMsg.setStatus(500);
            Msg.add("检测到token为空!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        Boolean aBoolean = mapper.deleteWebRtcLiveStreamingInfoByToken(token);
        if (!aBoolean){
            log.warn("通过token删除用户WebRtc直播信息失败!token:{}",token);
            returnMsg.setStatus(500);
            Msg.add("删除用户WebRtc直播信息失败!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        log.info("通过token删除用户WebRtc直播信息成功.");
        returnMsg.setStatus(200);
        return returnMsg;
    }

    @Override
    public LiveStreamingManagerReturnMsg getWebRtcLiveStreamingInfoByToken(String token) {
        LiveStreamingManagerReturnMsg returnMsg = new LiveStreamingManagerReturnMsg();
        List<String> Msg = new ArrayList<>();
        log.info("即将通过token获取WebRtc直播信息.token:{}",token);
        LiveStreamingManagerInfo webRtcLiveStreamingInfoByToken = mapper.getWebRtcLiveStreamingInfoByToken(token);
        if (webRtcLiveStreamingInfoByToken==null){
            log.warn("未能通过token找到对应WebRtc信息.token:{},返回为空!",token);
            returnMsg.setStatus(500);
            Msg.add("未能通过token找到对应直播信息!");
            returnMsg.setMsg(Msg);
            return returnMsg;
        }
        returnMsg.setData(webRtcLiveStreamingInfoByToken);
        returnMsg.setStatus(200);
        returnMsg.setMsg(Msg);
        return returnMsg;
    }

    private String createWebRtcAddress(String token){
        if (token==null){
            log.warn("token值为空!");
            return null;
        }
        return "webrtc://115.158.28.160/live/"+token;
    }
}
